<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/8/22
 * Time: 4:25
 * QQ:1467572213
 */

namespace app\en\model;

use app\index\model\Recharge as Recharge_m;
use think\Model;
use think\Db;
use think\facade\Config;
use think\facade\Request;

class Recharge extends Model
{
    public static function recharge_api()
    {
        return Db::name('fd_recharge_api')->select();
    }

    public static function oceanpay($user_info,$money)
    {
        $oceanpay_url = Config::get('oceanpay_url');
        $oceanpay_code = Config::get('oceanpay_code');
        $oceanpay_key = Config::get('oceanpay_key');
        $oceanpay_paycode = Config::get('oceanpay_paycode');

        $merordercode = time().mt_rand(11111,99999); //订单号
        $callbackurl = Request::domain().'/index/notify/oceanpay';
        $notifyurl = Request::domain().'/index/recharge/index';

        $sign_data['code'] = $oceanpay_code;
        $sign_data['merordercode'] = $merordercode;
        $sign_data['notifyurl'] = $notifyurl;
        $sign_data['callbackurl'] = $callbackurl;
        $sign_data['amount'] = $money;
        $sign_data['key'] = $oceanpay_key;
        $sign = self::oceanpay_sign($sign_data);
        $params['code'] = $oceanpay_code;
        $params['paycode'] = $oceanpay_paycode;
        $params['notifyurl'] = $notifyurl;
        $params['amount'] = $money;
        $params['callbackurl'] = $callbackurl;
        $params['merordercode'] = $merordercode;
        $params['signs'] = $sign;
        $params['starttime'] = self::getMillisecond();
        $res = self::oceanpay_post($oceanpay_url,$params);
        if ($res['success'] == true){
            $info['order_id'] = $merordercode;
            $info['uid'] = $user_info['id'];
            $info['pid'] = $user_info['pid'];
            $info['username'] = $user_info['username'];
            $info['phone'] = $user_info['phone'];
            $info['money'] = $money;
            $info['service_charge'] = $money * Base::get_config('service_cz') / 100;
            $info['type'] = 1;
            $info['status'] = 0;
            $info['create_time'] = time();
            $add = Db::name('fd_recharge')->insert($info);
            if ($add == true){
                return ['code'=>1,'data'=>$res['data']['checkstand']];
            }else{
                return ['code'=>0,'msg'=>'系统错误'];
            }
        }else{
            return ['code'=>0,'msg'=>'接口失败'];
        }
    }

    public static function oceanpay_sign($sign_data)
    {
        $hmacstr = 'code='.$sign_data['code'].'&merordercode='.$sign_data['merordercode'].'&notifyurl='.$sign_data['notifyurl'].'&callbackurl='.$sign_data['callbackurl'].'&amount='.$sign_data['amount'].'&key='.$sign_data['key'].'';
        $sign= strtoupper(md5($hmacstr));
        return $sign;
    }

    public static function oceanpay_post($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $output = curl_exec($ch);
        curl_close($ch);
        $rst = json_decode($output,true);
        return $rst;
    }



    public static function shineupay($user_info,$money)
    {
        $shineupay_id = Config::get('shineupay_id');
        $shineupay_key = Config::get('shineupay_key');
        $notifyUrl = Request::domain().'/index/notify/shineupay';
        $redirectUrl = Request::domain().'/index/recharge/index';

        //参数数据处理
        $url                = Config::get('shineupay_url'); //网关地址
        $params["orderId"]  = time().mt_rand(11111,99999); //订单号
        $params["amount"]   = $money; //支付金额
        $getMillisecond     = self::getMillisecond(); //毫秒时间戳
        $params["details"]  = "details_pay";
        $params["userId"]   = mt_rand(1111,9999);
        $params["notifyUrl"]   = $notifyUrl;
        $params["redirectUrl"]   = $redirectUrl;
        //组装数据
        $data['body']       = $params;
        $data['merchantId'] = $shineupay_id;
        $data['timestamp']  = $getMillisecond;

        //报文签名
        $sign = self::sign($shineupay_key, $data);
        //封装请求头
        $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache", "Api-Sign:$sign");
        //发起post请求
        $json = Recharge_m::shineupay_post($url, $data, 5, $headers, $getMillisecond);
        $res = json_decode($json, true);
        if ($res['status'] == 0 && $res['merchantId'] == $shineupay_id){
            $order_info['order_id'] = $params["orderId"];
            $order_info['p_order_id'] = $res['body']['transactionId'];
            $order_info['uid'] = $user_info['id'];
            $order_info['pid'] = $user_info['pid'];
            $order_info['username'] = $user_info['username'];
            $order_info['phone'] = $user_info['phone'];
            $order_info['money'] = $money;
            $order_info['service_charge'] = $money * Base::get_config('service_cz') / 100;
            $order_info['type'] = 1;
            $order_info['status'] = 0;
            $order_info['create_time'] = time();
            $add_order = Db::name('fd_recharge')->insert($order_info);
            if ($add_order == true){
                return $res['body']['content'];
            }else{
                return 'error';
            }
        }else{
            return 'error';
        }
    }


    public static function sign($key, $data)
    {
        $data = array_filter($data);
        $str = json_encode($data) . "|" . $key;
        $sign = MD5($str);
        return $sign;
    }

    //获取毫秒时间戳
    public static function getMillisecond()
    {
        list($microsecond, $time) = explode(' ', microtime()); //' '中间是一个空格
        return (float)sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
    }

    public static function shineupay_post($url, $data, $timeout, $headers, $getMillisecond)
    {
        $data = json_encode($data);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl); //捕抓异常
        }
        curl_close($curl);
        return $output;
    }

    public static function Global_pay($user_info,$money)
    {
        $post_data['merch_id'] = '80';
        $post_data['payment_id'] = 3;
        $post_data['order_sn'] = time().mt_rand(11111,99999); //订单号
        $post_data['amount'] = $money;
        $post_data['notify_url'] = Request::domain().'/index/notify/global_pay';
        //按字典正序排序传入的参数
        ksort($post_data);

        $sign_str='';
        foreach($post_data as $pk=>$pv){
            $sign_str.="{$pk}={$pv}&";
        }
        $sign_str.="key=9716841d1b11cb39c32599f79b686a48f4a1e01f";
        $post_data['sign']=md5($sign_str);

        //开始提交--------------------------------------------------------------
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://globalpay.pw/api/payTest/order');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $output = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($output,true);
        if ($result['code'] == 1){
            $order_info['order_id'] = $result['data']['order_sn'];
            $order_info['p_order_id'] = $result['data']['pt_order_sn'];
            $order_info['uid'] = $user_info['id'];
            $order_info['pid'] = $user_info['pid'];
            $order_info['username'] = $user_info['username'];
            $order_info['phone'] = $user_info['phone'];
            $order_info['money'] = $money;
            $order_info['service_charge'] = $money * Base::get_config('service_cz') / 100;
            $order_info['type'] = 1;
            $order_info['status'] = 0;
            $order_info['create_time'] = time();
            $add_order = Db::name('fd_recharge')->insert($order_info);
            if ($add_order == true){
                return ['code'=>1,'msg'=>'下单成功','data'=>$result['data']['pay_pageurl']];
            }else{
                return ['code'=>0,'msg'=>'系统错误,请联系客服!'];
            }
        }else{
            return ['code'=>0,'msg'=>'创建订单失败'];
        }
    }


}